//// onload
//$(document).ready(function() {
//	device.view(deviceId);
//});

function oneDeviceData(deviceId) {
	alert(deviceId);
};

var device = {
	deviceUrl : contextPath + '/device'

	//데이터 상세보기 조회
	,view : function(deviceId) {
		var request = $.ajax({
			url : this.deviceUrl + '/' + deviceId
			,type : 'GET'
			,dataType : 'json'
		});
		request.done(function(data) {
			var html = [];
			html.push('<form id="update" method="POST" onsubmit="device.update(); return false;">');
			html.push('<table>');
			html.push('<tr><th></th><td><input type=hidden id="id" name="id" value=' + data["id"] + '></td></tr>');
			html.push('<tr><th align=left>DEVICE NAME</th><td><input type="text" id="name" name="name" value=' + data["name"] + '></td></tr>');
			html.push('<tr><th align=left>LONGITUDE</th><td><input type="text" id="longitude" name="longitude" value=' + data["longitude"] + '></td></tr>');
			html.push('<tr><th align=left>LATITUDE</th><td><input type="text" id="latitude" name="latitude"; value=' + data["latitude"] + '></td></tr>');
			html.push('</table>');
			html.push('<input type="submit" value="수정" >');
			html.push('<input type="button" value="취소" onclick="top.window.close()" >');
			$('#updateDevice').html(html.join(""));
		});
		request.fail(function(jqXHR, textStatus) {
			alert('Request failed: ' + textStatus);
		});
	}
	//데이터 등록
	,update : function() {
		if ( $.trim($("#name").val()) == "" ) {
			alert("DEVICE NAME을 입력하세요.");
			$("#name").focus();
			return;
		}
		else if ( $.trim($("#longitude").val()) == "" ) {
			alert("LONGITUDE를 입력하세요.");
			$("#longitude").focus();
			return;
		}
		else if ( $.trim($("#latitude").val()) == "" ) {
			alert("LATITUDE를 입력하세요.");
			$("#latitude").focus();
			return;
		}
		// form 데이터 json 으로 변환
		var json = $.toJSON($('#update').serializeJSON());
		alert("data:" + json);
		var request = $.ajax({
			url : this.deviceUrl
			,type : 'PUT'
			,contentType : 'application/json; charset=UTF-8'
			,data : json
			,dataType : 'json'
		});
		request.done(function(data) {
			//팝업창 닫고 부모창 refresh
			opener.location.reload();
			top.window.close();
		});
		request.fail(function(jqXHR, textStatus) {
			alert('Request failed: ' + textStatus);
		});
	}
};